﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Net.Http.Headers;
using TPLOAWebClient.Resources;
using System.Web.Security;

namespace TPLOAWebClient
{
    public partial class staffmodify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string loaid = Request.QueryString["details"];
                HttpClient client = (HttpClient)Session["httpclient"];
                HttpResponseMessage response = null;
                HttpResponseMessage response2 = client.GetAsync("api/LOAs/" + loaid).Result;  // Blocking call!
                if (!Page.IsPostBack)
                {
                    if (response2.IsSuccessStatusCode)
                    {
                        // Parse the response body. Blocking!
                        var loa = response2.Content.ReadAsAsync<LOA>().Result;
                        adminnoLbl.Text = loa.StudentAdminNo;
                        loaidLbl.Text = Convert.ToString(loa.LOAID);
                        datesubmitLbl.Text = loa.SubmitDateTime;
                        datefromLbl.Text = loa.StartDateTime;
                        datetoLbl.Text = loa.EndDateTime;
                        reasonLbl.Text = loa.LOAReason;
                        if (loa.GradedAssignmentClash == true)
                        {
                            gradedassignmentLbl.Text = "Yes";
                        }
                        else
                        {
                            gradedassignmentLbl.Text = "No";
                        }
                        gradedassignmentdetailsLbl.Text = loa.GradedAssignmentClashDetails;
                        RadioButtonList1.SelectedValue = loa.ReviewStatus;
                        if (RadioButtonList1.SelectedValue == "Rejected")
                        {
                            rejectreasonTbx.Visible = true;
                            RequiredFieldValidator1.Enabled = true;
                            rejectreasonTbx.Text = "";
                            Panel1.Visible = true;
                        }
                        else
                        {
                            rejectreasonTbx.Visible = false;
                            RequiredFieldValidator1.Enabled = false;
                            Panel1.Visible = false;
                        }
                        rejectreasonTbx.Text = loa.RejectReason;
                        mcLbl.Text = Convert.ToString(loa.MCID);
                    }
                    datereviewedLbl.Text = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() +
                            " " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
                }
                    if (Session["username"] != null)
                    {
                        response = client.GetAsync("api/members/" + Session["username"]).Result;
                        var member = response.Content.ReadAsAsync<Member>().Result;
                        if (member.UserType.Equals("Student"))
                        {
                            Response.Redirect("studentprofile.aspx");
                        }
                        else if (member.UserType.Equals("Clinic"))
                        {
                            Response.Redirect("clinicprofile.aspx");
                        }
                        else if (member.UserType.Equals("Admin"))
                        {
                            Response.Redirect("adminprofile.aspx");
                        }

                        response = client.GetAsync("api/Staffs/" + Session["username"]).Result;
                        var staff = response.Content.ReadAsAsync<Staff>().Result;
                        if (!staff.ApprovePermissions)
                        {
                            Response.Redirect("staffprofile.aspx");
                        }

                    }
            
        }

        protected void modifyBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                HttpClient client = (HttpClient)Session["httpclient"];

                //string type = "LOA";
                //if (mcLbl.Text != "0")
                //{
                  //  type = "MC";
                //}
                bool gradedassignment = false;
                if (gradedassignmentLbl.Text == "Yes")
                {
                    gradedassignment = true;
                }
                var loa = new LOA()
                {
                    LOAID = Convert.ToInt32(loaidLbl.Text),
                    StartDateTime = datefromLbl.Text + " " + timefromLbl.Text,
                    EndDateTime = datetoLbl.Text + " " + timetoLbl.Text,
                    LOAReason = reasonLbl.Text,
                    GradedAssignmentClash = gradedassignment,
                    GradedAssignmentClashDetails = gradedassignmentdetailsLbl.Text,
                    ReviewStatus = RadioButtonList1.SelectedValue,
                    RejectReason = rejectreasonTbx.Text,
                    ReviewDateTime = datereviewedLbl.Text,
                };
                HttpResponseMessage response = client.PutAsJsonAsync("api/LOAs/" + loaidLbl.Text, loa).Result;  // Blocking call!
                if (response.IsSuccessStatusCode)
                {
                    resultLbl.Text = "LOA Updated at "+DateTime.Now.ToLongDateString()+" "+DateTime.Now.ToLongTimeString();
                }
                else
                {
                    resultLbl.Text = "Error code:" + response.StatusCode + "reason:" + response.ReasonPhrase.ToString() + Environment.NewLine;
                }
            }
        }

        protected void profileBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffprofile.aspx");
        }

        protected void viewloaBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffviewloa.aspx");
        }

        protected void submitloaBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffsubmitloa.aspx");
        }

        protected void faqBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("stafffaq.aspx");
        }

        protected void otherservicesBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("staffotherservice.aspx");
        }

        protected void LogoutBtn_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("default.aspx");
        }

        protected void deleteBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                HttpClient client = (HttpClient)Session["httpclient"];
                string loaid = loaidLbl.Text;

                HttpResponseMessage response = client.DeleteAsync("api/LOAs/" + loaid).Result;
                if (response.IsSuccessStatusCode)
                {
                    Response.Redirect("staffviewloa.aspx?status=success&LOAID="+loaidLbl.Text);
                }
                else
                {
                    Response.Redirect("staffviewloa.aspx?status=error&LOAID="+loaidLbl.Text);
                }
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedValue == "Rejected")
            {
                rejectreasonTbx.Visible = true;
                RequiredFieldValidator1.Enabled = true;
                rejectreasonTbx.Text = "";
                Panel1.Visible = true;
            }
            else 
            {
                rejectreasonTbx.Visible = false;
                RequiredFieldValidator1.Enabled = false;
                Panel1.Visible = false;
            }
        }

        protected void rejectreasonTbx_TextChanged(object sender, EventArgs e)
        {

        }
    }
}